﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layedit', 'layer', 'table', 'tree', 'jquery', 'formSelects'], function () {
    var form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        player = parent.layer || layui.layer
        tree = layui.tree,
        formSelects = layui.formSelects; //很重要;

    var authorityUserModifyLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                uploadImgUrl: "/Admin/Home/UploadImg",
                sysPostAllListUrl: "/Authority/Post/LoadPostSelectList",
                departmentAllListUrl: "/Authority/Department/LoadDepartmentSelectList",
                sysRoleAllListUrl: "/Authority/Role/LoadRoleSelectList",

                saveUrl: "/Authority/User/ModifyInfo",
            };

            this.initPage();

            this.bindEvent();

        },
        //初始化列表
        initPage: function () {

            //获取信息
            var data = GrdAssist.queryString("data");
            self.logicFunc.loadInfo(data);

            form.render();
        },
        //绑定事件
        bindEvent: function () {
            //上传图片
            $(".userFaceBtn").on("click", this.logicFunc.uploadImg);

            //监听立即修改提交
            form.on('submit(modifyButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);
        },
        //逻辑方法
        logicFunc: {

            //获取修改的信息
            loadInfo: function (data) {

                var jsonData = null;
                if (typeof data == "string") {
                    jsonData = JSON.parse(decodeURIComponent(data));
                } else {
                    jsonData = data;
                }
                $("#userFace").attr("src", jsonData.sPicUrl); //头像
                $("input[name=gCode]").val(jsonData.gCode); //gCode
                $("input[name=sLoginName]").val(jsonData.sLoginName); //账号
                $("input[name=sUserName]").val(jsonData.sUserName); //名称
                $("input[name=iSex][value=" + jsonData.iSex + "]").attr("checked", true);//性别
                $("input[name=sPhone]").val(jsonData.sPhone); //电话号码
                $("input[name=iState][value=" + jsonData.iState + "]").attr("checked", true);//状态

                if (jsonData.bIsSuperAdmin == true) {
                    
                    $("#sRoleList").attr('readonly', true);
                    $("#sDepartmentList").attr('readonly', true);
                    $("#sPostList").attr('readonly', true);

                } else {

                    //所属角色
                    var sRoleList = new Array();
                    if (jsonData.sRoleCodes != null) {
                        var formSelect = layui.formSelects;
                        sRoleList = jsonData.sRoleCodes.split(",");
                        formSelect.value('sRoleList', sRoleList);
                    }
                    self.logicFunc.loadSysRoleAllList(sRoleList);

                    //所属部门
                    var sDepartmentList = new Array();
                    if (jsonData.sDepartmentCodes != null) {
                        sDepartmentList = jsonData.sDepartmentCodes.split(",");
                        formSelects.value('sDepartmentList', sDepartmentList);
                    }
                    self.logicFunc.loadDepartmentAllList(sDepartmentList);

                    //所属岗位
                    var sPostList = new Array();
                    if (jsonData.sPostCodes != null) {
                        sPostList = jsonData.sPostCodes.split(",");
                        formSelects.value('sPostList', sPostList);
                    }
                    self.logicFunc.loadSysPostAllList(sPostList);
                }
            },

            //获取所有角色
            loadSysRoleAllList: function (dataInfo) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.sysRoleAllListUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            //local模式
                            formSelects.data('sRoleList', 'local', {
                                arr: result.Data
                            });
                            formSelects.value('sRoleList', dataInfo);

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //获取部门列表信息
            loadDepartmentAllList: function (dataInfo) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.departmentAllListUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            formSelects.data('sDepartmentList', 'local', {
                                arr: result.Data
                            });
                            formSelects.value('sDepartmentList', dataInfo);
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //获取所有的岗位
            loadSysPostAllList: function (dataInfo) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.sysPostAllListUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            //local模式
                            formSelects.data('sPostList', 'local', {
                                arr: result.Data
                            });
                            formSelects.value('sPostList', dataInfo);
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //上传图片
            uploadImg: function (obj) {

                var callback_name = "loadUploadImgPage_callback_" + new Date().getTime();
                window[callback_name] = function (name, fullUrl) {

                    $(".userFaceBtn").attr({ src: fullUrl + "?v=" + Math.random(), "data-savesrc": fullUrl });
                };

                var index = layui.layer.open({
                    title: "图片上传",
                    type: 2,
                    resize: false,
                    area: ['820px', '650px'],
                    content: self.Parameters.uploadImgUrl + '?' + $.param({
                        imageType: "UserInfo",
                        callback: callback_name,
                        width: 500,
                        height: 350
                    }),
                    end: function () {
                        delete window[callback_name];
                    }
                });
            },

            //保存数据
            saveInfo: function () {
                var json = {
                    parameters: GrdLayUI.initParamsData($("#modify-form"))
                };

                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {

                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },
            //取消
            close: function () {
                player.closeAll();
            }
        },
        //工具
        tools: {

        }
    };
    authorityUserModifyLogic.init();
});
